package com.qdlc.p2p.biz.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.AccountLogService;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dao.AccountDao;
import com.qdlc.p2p.dal.dao.AccountLogDao;
import com.qdlc.p2p.dal.dao.DictDao;
import com.qdlc.p2p.dal.dto.Account;
import com.qdlc.p2p.dal.dto.AccountLog;
import com.qdlc.p2p.dal.model.AccountLogModel;

/**
 * 资金记录
 *
 * @author wuaj
 * @version 1.1.0.0
 * @since 2016年3月17日
 */
@Service("accountLogService")
public class AccountLogServiceImpl implements AccountLogService {

	@Resource
	private AccountLogDao accountLogDao;

	@Resource
	private DictDao dictDao;

	@Resource
	private AccountDao accountDao;

	@Override
	public void add(AccountLog log) {
		accountLogDao.save(log);
	}

	@Override
	public PageDataList<AccountLogModel> list(AccountLogModel model) {
        int pageNumber = model.getPage();
		int pageSize = model.getRows()> 0 ? model.getRows():10;
		PageDataList<AccountLogModel> pageDateList =  accountLogDao.list(model, pageNumber, pageSize);
		/*PageDataList<AccountLogModel> pageDateList_ = new PageDataList<AccountLogModel>();
		List<AccountLogModel> list = new ArrayList<AccountLogModel>();
		pageDateList_.setPage(pageDateList.getPage());
		if (pageDateList.getList().size() > 0) {
			for (int i = 0; i < pageDateList.getList().size(); i++) {
				AccountLogModel c = pageDateList.getList().get(i);
				AccountLogModel cm = AccountLogModel.instance(c);
     			Dict d = dictDao.find("account_type", c.getType());
				if (d != null) {
					cm.setTypeName(d.getName());
   			    }
//				if (c.getToUser() != null) {
//					cm.setToUser(c.getToUser());
//					cm.setToUserName(c.getToUser().getUserName());
//				}
				list.add(cm);
			}
		}
	    pageDateList_.setList(list);*/
		return pageDateList;
	}

	@Override
	public PageDataList<AccountLogModel> accountLogList(AccountLogModel model, int pageNumber, int pageSize) {
		return accountLogDao.accountLogList(model, pageNumber, pageSize);
	}

	@Override
	public List<AccountLogModel> accountTransactionLog(long userId) {
		return accountLogDao.findAccountLogByUserId(userId, 4);
	}

	@Override
	public double todayEarnAmount(long userId) {
		return accountLogDao.todayEarnAmount(userId);
	}

	@Override
	public double earnAmount(long userId) {
		return accountLogDao.earnAmount(userId);
	}

	@Override
	public double earnAmount() {
		return accountLogDao.earnAmount();
	}

	@Override
	public double[] amount(String type, String year) {
		double[] amounts = new double[12];
		String date;
		for (int i = 0; i < 12; i++) {
			date = year + (i > 8 ? "-" : "-0") + (i + 1);
			amounts[i] = accountLogDao.amount(type, date);
		}
		return amounts;
	}

	/**
	 * 增加资金变更日志
	 *
	 * @param userId
	 * @param toUserId
	 * @param amount
	 * @author Kaiju Zhao
	 */
	@Override
	public void addAccountLog(long userId, long toUserId, double amount) {
		Account account = accountDao.findByUserId(userId);
		this.accountLogDao.addAccountLog(account, toUserId, amount);
	}
}
